<template>
	<view class="container plr_28 fc">
		<view class="mt_32">
			<button class="custom_btn block_btn" open-type="chooseAvatar" @chooseavatar="chooseavatar">
				<image class="ib width_80 height_80 br50" :src="avatarUrl" mode="aspectFit"></image>
			</button>
		</view>
		<view class="mt_32 fz_16">
			<view class="fx fl pb_12 bob_1 boc_grey">
				<view class="fb width_98 fx_sh">
					昵称
				</view>
				<input @change="nickNameChange" v-model="nickName" placeholder="请输入昵称" class="fx_1" type="nickname" />
			</view>
			<view class="fx fl pb_12 bob_1 boc_grey mt_16" v-if="!isRegist">
				<view class="fb width_98 fx_sh">
					邀请人ID
				</view>
				<input v-model="shareId" placeholder="请输入邀请人ID" class="fx_1" type="text" />
			</view>
		</view>
		<view class="mt_64">
			<button class="w_100 custom_btn lh_44 primary br_57 fz_14" @click="bindLogin">确定</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import api from "@/service/config/api";
	const defaultAvatarUrl =
		'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

	export default {
		data() {
			return {
				isRegist: false,
				avatarUrl: defaultAvatarUrl,
				nickName: "",
				phoneCode: "",
				code: "",
				phone: "",
				backUrl: "/pages/index/index",
				shareId: ""
			}
		},
		onLoad(options) {
			console.log("info页面")
			console.log(options)
			this.phone = options.phone
			this.code = options.code
			this.backUrl = options.backUrl ? options.backUrl : "/pages/index/index";
			// this.getPhoneNumber()
		},
		onShow() {
			const pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			if (currentPage.$vm && currentPage.$vm.isFromBack) {
				console.log("通过返回按钮进入");
				this.phone = ""
				this.code = ""
				this.shareId = ""
				this.isRegist = false
				this.nickName = ""
			}
		},
		onUnload() {
			// 当 PageB 页面卸载时，清空 PageA 页面中输入框的内容
			let pages = getCurrentPages(); // 获取页面栈
			let prevPage = pages[pages.length - 2]; // 上一个页面
			if (prevPage && prevPage.reloadPage) {
				prevPage.reloadPage(); // 调用上一个页面的方法清空输入框
			}
		},
		computed: {
			...mapState(['appId']),
		},
		methods: {
			reloadPage() {
				console.log("通过返回按钮进入")
				this.phone = ""
				this.code = ""
				this.shareId = ""
				this.isRegist = false
				this.nickName = ""
			},
			// 获取用户手机号码
			getPhoneNumber() {
				api.post(`/user/api/auth/getWechatPhone/${this.phoneCode}`).then(res => {
					console.log(111)
					console.log(res)
					if (res && res.code == 0) {
						this.phone = res.data.phone
						this.code = res.data.code
					}
					api.get("/user/api/auth/exit/" + this.phone).then((res) => {
						console.log(res);
						this.isRegist = res.data

					});
				})
			},
			// 用户注册
			userRegist(type) {
				api
					.post(
						"/user/api/auth/phoneRegister", {
							phone: this.phone,
							code: this.code,
							avatar: this.avatarUrl,
							nickName: this.nickName,
							shareId: this.shareId,
						}, {
							isLogin: true,
						}
					)
					.then((res) => {
						// if (res.code == 1) {
						// 	return uni.$u.toast(res.msg);
						// }
						// if (res.data) {
						// 	this.userLogin();
						// }

						if (res && res.user_id) {
							this.$storage.setItem("token", res.access_token);
							this.$store.commit('setToken', res.access_token)
							this.$storage.setItem("tokenType", res.token_type);
							this.$storage.setItem("refreshToken", res.refresh_token);
							this.getBasicUserInfo()
						} else {
							return uni.$u.toast(res.msg);
						}
						// if (res && res.code == 1) {
						// 	return uni.$u.toast(res.msg);
						// }
					});
			},
			// 登录接口
			userLogin(type) {
				console.log("用户登录");
				// auth/oauth2/token?grant_type=mobile&scope=server&mobile=APP-SMS@13838384384&code=1234' \

				api
					.post(
						`/auth/oauth2/token?grant_type=mobile&scope=server&mobile=APP-SMS@${this.account}&code=${this.captcha}`, {
							// grant_type: "mobile",
							// scope: "server",
							// mobile: this.account,
							// code: this.captcha
						}, {
							isLogin: true,
						}
					)
					.then((res) => {
						console.log("登录结果");
						// this.$storage.setItem("token",res)
						console.log(res);
						// if (res && res.code) {
						// 	uni.$u.toast(res.msg);
						// 	return;
						// }
						if (res && res.user_id) {
							this.$storage.setItem("token", res.access_token);
							this.$storage.setItem("tokenType", res.token_type);
							this.$storage.setItem("refreshToken", res.refresh_token);
							this.$store.commit('setToken', res.access_token)
							this.getBasicUserInfo()
						} else {
							return uni.$u.toast(res.msg);
						}
					});
			},
			bindPage() {
				console.log("页面跳转")
				console.log(this.backUrl)
				uni.reLaunch({
					url: this.backUrl
				})
			},
			getBasicUserInfo() {
				api.get("/user/api/auth/userInfo").then((res) => {
					console.log("用户信息1");
					console.log(res);
					if (res && res.code == 0) {
						const data = res.data
						let userType = data.identityType

						if (data.identityType == 1) {
							if (data.dealer) {

								this.$storage.setItem("userType", 3);
								userType = 3
							} else if (!data.manager && !data.guide && !data.dealer) {

								this.$storage.setItem("userType", 4);
								userType = 4
							} else if (data.manager) {
								this.$storage.setItem("userType", 2);
								userType = 2

							} else if (data.guide) {
								this.$storage.setItem("userType", 1);
								userType = 1

							}

						} else if (data.identityType == 2) {
							this.$storage.setItem("userType", 3);
							userType = 3
						} else {
							this.$storage.setItem("userType", data.identityType + 2);
							userType = data.identityType + 2
						}
						console.log('userTypeuserType,', userType)
						this.$store.commit('setUserType', userType)
						if (data) {
							this.$store.commit('setUserInfo', data)
							this.$storage.setItem("basicUnserInfo", data)
							const platformInfo = uni.getStorageSync("platformInfo")
							console.log(platformInfo)
							if (platformInfo.tenantType == 0) {
								console.log("平台小程序的登录流程")
								console.log(platformInfo)
								console.log(data)
								if (userType == 3 && !data.dealerAuditStatus) {
									uni.navigateTo({
										url: "/pages/index/introduce"
									})
								} else {
									this.bindPage();
								}
							} else {
								this.bindPage();
							}
							// if (this. == "wxdd90ffc3848954d8") {
							// 	if (userType == 3 && !data.dealerAuditStatus) {
							// 		uni.navigateTo({
							// 			url: "/pages/index/introduce"
							// 		})
							// 	} else {
							// 		this.bindPage();
							// 	}

							// 	console.log("平台小程序")
							// 	console.log(data)
							// } else {
							// 	this.bindPage();

							// }
						}
					} else {

					}
				});
			},
			chooseavatar(e) {
				console.log("头像选择")
				console.log(e)
				const {
					avatarUrl
				} = e.detail
				this.avatarUrl = avatarUrl
			},
			nickNameChange(e) {
				console.log(e)

				this.nickName = e.detail.value

				// uni.showToast({
				// 	title:e.detail.value
				// })
			},
			bindLogin() {
				console.log(this.avatarUrl)
				console.log(this.nickName)
				if (this.avatarUrl && this.nickName) {
					console.log("用户信息完整")
					if (!this.shareId) {
						return this.$utils.showToast("请输入邀请人ID");
					}
					this.userRegist()
					// uni.login({
					// 	success(res) {
					// 		console.log("微信登录")
					// 		console.log(res)
					// 	}
					// })
				} else {
					uni.$u.toast("请填写完整的信息");
				}
			}
		}
	}
</script>

<style lang="scss">
	@include color_page;
</style>

<style lang="scss" scoped></style>